Page({
  data: {
    // 茶类
    items: [
      { name: '绿茶', id: "2", checked: 'checked'},
      { name: '红茶', id: "1"},
      { name: '白茶', id: "3"},
      { name: '普洱茶', id: "4"},
      { name: '乌龙茶', id: "5"},
      { name: '黑茶', id: "6"},
    ],
    // 销售额选择
    casIndex: 0,
    casArray: ['100万元以下', '100~300万元', '300~1000万元', '1000~3000万元', '3000~10000万元', '1~3亿元','3亿元以上'],
    chanqu:[
      // 产区
    ],
    bangming: [
      // 榜名
      { id: "1", name: "匠人", checked: 'checked' },
      { id: "2", name: '品牌' },
      { id: "3", name: '珍品' },
    ],
    ry:[
      { id: "1", name: '正山出品', checked: '' }, { id: "2", name: '古树老丛', checked: '' }, { id: "3", name: '传统工艺', checked: '' }, { id: "4", name: '大师手制', checked: '' },
    ],
    wechat: [
      { id: "1", name: "微信", checked: 'checked' },
      { id: "2", name: '手机' },
    ],
    manandgril: [
      { id: "1", name: "男士", checked: 'checked' },
      { id: "2", name: '女士' },
    ],
    rr:[],
    sex:"1",
    contact:"1",
    dats_ry:[],
    nametiao:"none",
    xiantiao:false,
    bd_id:1,
    pp:'none',
    jr:'block',
    zp:"none"
  },
  //男女选择
  manandgrilchoose: function (e) {
    var id = e.detail.value;
    var pos = this.data.manandgril;
    pos = this.ss(pos, id);
    this.setData({
      sex:id,
      manandgril: pos,
    })
  },
  //联系方式选择
  wechatchoose: function (e) {
    var id = e.detail.value;
    var pos = this.data.wechat;
    pos = this.ss(pos, id);
    this.setData({
      contact:id,
      wechat: pos,
    })
  },
  // 判断词条是否存在
  iscture:function(e){
    var name = e.detail.value;
    var that = this;
    if(!name){
      that.setData({
        nametiao: "none",
      })
      return;
    }
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/search_ct',
      method: 'GET',
      data: {
        name: "" + name,
      },
      success: function (res) {
        if(res.data==1){
          var issts = true;
        }else{
          var issts = false;
        }
        that.setData({
          nametiao: 'block',
          xiantiao: issts,
        })
      }
    })
  },
  // 茶类
  radioChange:function(e){
    var that = this;
    var id = e.detail.value;
    var pos = that.data.items;
    pos = that.ss(pos, id);
    that.setData({
      items: pos,
    })
    wx.request({
      // 选择请求茶类
      url: getApp().AppUrl + 'index.php/api/api/getTeaArea',
      method: 'GET',
      data: {
        id: "" + id
      },
      success: function (res) {
        console.log(res.data)
        var chanqu = res.data;
        if (chanqu) {
          for (var index in chanqu) {
            chanqu[0].checked = 'checked';
          }
        }
        that.setData({
          chanqu: chanqu
        })
      }
    })
    
  },
  //产区选择
  chaquchoose:function(e){
    var id = e.detail.value;
    var pos = this.data.chanqu;
    for (var index in pos) {
      if (pos[index].teaarea_id == id) {
        pos[index].checked = 'checked';
      } else {
        pos[index].checked = '';
      }
    }
    this.setData({
      chanqu: pos,
    })
  },
  // 荣誉多选
  rychoose:function(e){
    var that = this;
    var ry = that.data.ry;
    var ids = parseInt(e.target.dataset.id);
    var rr = that.data.rr;
    for(var index in ry){
      if (ry[index].id == ids){
        if (ry[index].checked == ''){
          ry[index].checked = 'checked';
        }else{
          ry[index].checked = ''
        }
      }
    }
    
    if(rr.length>0){
      var index = rr.indexOf(ids);
      if(index>-1){
        rr.splice(index, 1);
      }else{
        rr.push(ids);
      }
    }else{
      rr.push(ids);
    }
    that.setData({
      ry: ry,
      rr: rr
    })
    // console.log('发生change事件，携带value值为：', ids)
  },
  // 榜单选择
  bangmingchoose:function(e){
    var id = e.detail.value;
    var pos = this.data.bangming;
    var ry = this.data.ry;
    if (id == 2) {
      var jr = 'none';
      var pp = 'block';
      var zp = 'none';
      for(var index in ry){
        ry[index].checked = '';
      }
    } else if (id == 3) {
      var jr = 'none';
      var pp = 'none';
      var zp = 'block';
    } else {
      var jr = 'block';
      var pp = 'none';
      var zp = 'none';
      for (var index in ry) {
        ry[index].checked = '';
      }
    }
    pos = this.ss(pos, id);
    
    this.setData({
      bangming: pos,
      bd_id:id,
      jr: jr,
      pp: pp ,
      zp: zp,
      rr:[],
    })
  },
  locationlw: function () {
    wx.navigateTo({
      url: '../Contact/Contact'
    })
  },
  locationdesc: function () {
    wx.navigateTo({
      url: '../Description/Description'
    })
  },
  // 共用函数选择id
  ss: function (pos,id) {
    for (var index in pos) {
      if (pos[index].id == id) {
        pos[index].checked = 'checked';
      } else {
        pos[index].checked = '';
      }
    }
    return pos;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    });
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    // 请求茶类
    wx.request({
      url: getApp().AppUrl + '/index.php/api/api/getTeaCase',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var dats = res.data;
        dats= that.ss(dats,1);
        that.setData({ 
          items: dats,
          }) 
      }
    })
    wx.request({
      // 请求茶类
      url: getApp().AppUrl + 'index.php/api/api/getTeaArea',
      method: 'GET',
      data: {
        id: "" + 1
      },
      success: function (res) {
        var chanqu = res.data;
        wx.hideLoading()
        if(chanqu){
          for (var index in chanqu) {
            chanqu[0].checked = 'checked';
          }
        }
        that.setData({
          chanqu: chanqu
        })
      }
    })
  },
  // 表单提交
  formSubmit:function(e){
    var datas = e.detail.value;
    var contact = this.data.contact;
    var rr = this.data.rr;
    var sex = this.data.sex;
    var xiantiao = this.data.xiantiao;
    datas["sex"] = sex;
    if (!datas['bangdan_id'] || !datas['teacase_id'] || !datas['teaarea_id'] || !datas['name']){
      wx.showToast({
        title: '词条内容不能为空',
        icon: 'fail',
        duration: 2000
      })
      return;
    }
    if(xiantiao){
      wx.showToast({
        title: '词条内容已存在',
        icon: 'fail',
        duration: 2000
      })
      return;
    }
    if(datas['bangdan_id']==3){
      for (var s = 0; s < rr.length; s++) {
        if (rr[s] == 1) {
          datas['zs'] = "√";
        }
        if (rr[s] == 2) {
          datas['gs'] = "√";
        }
        if (rr[s] == 3) {
          datas['ct'] = "√";
        }
        if (rr[s] == 4) {
          datas['ds'] = "√";
        }
      }
    }
    if (contact==1){
      datas['wechat'] = datas['lxfs'];
    }else{
      datas['phone'] = datas['lxfs'];
    }
    console.log(datas)
    var that = this;
    // 请求茶类
    wx.request({
      url: getApp().AppUrl + '/index.php/api/set/save_ct_name',
      data: datas,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        if(res.data==1){
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1000
          })
          
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            duration: 2000
          })
        }
        setTimeout(function () {
          wx.navigateBack({})
        }, 2000)
      },
    })
    console.log(datas)
    console.log('联系人'+this.data.contact)
    console.log('男女'+this.data.sex)
  },
  bindCasPickerChange: function (e) {
    console.log('销售额选的是', this.data.casArray[e.detail.value])
    if (e.detail.value == 4) {
      this.setData({ reply: true })
    } else {
      this.setData({ reply: false })
    }
    console.log(e.detail.value)
    this.setData({
      casIndex: e.detail.value
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})